package com.clkj.zhonghui.service.topic;

import com.clkj.zhonghui.dao.banner.BannerMapper;
import com.clkj.zhonghui.dao.topic.TagMapper;
import com.clkj.zhonghui.dao.topic.TopicCommentMapper;
import com.clkj.zhonghui.dao.topic.TopicMapper;
import com.clkj.zhonghui.dao.topic.TopicPriseMapper;
import com.clkj.zhonghui.pojo.banner.Banner;
import com.clkj.zhonghui.pojo.product.Product;
import com.clkj.zhonghui.pojo.product.ProductBanner;
import com.clkj.zhonghui.pojo.product.ProductFormat;
import com.clkj.zhonghui.pojo.topic.Topic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author：周佳润
 * @Date：2024/7/18 下午2:54
 */
@Service
public class ForumServiceImpl implements ForumService{

    @Resource
    private TopicMapper topicMapper;

    @Resource
    private TopicCommentMapper topicCommentMapper;

    @Resource
    private TopicPriseMapper topicPriseMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public Map getTopicAndBanner(String tagIds) {
        Map map = new HashMap();
        Example example = new Example(Topic.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topicFlag", 0);
        example.orderBy("topicId").asc();
        PageHelper.startPage(1, 10);
        map.put("topic",topicMapper.selectByExample(example));

        example =new Example(Topic.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("topicFlag", 1);
        example.orderBy("topicId").asc();
        PageHelper.startPage(1, 10);
        map.put("video",topicMapper.selectByExample(example));


        List<Integer> idList = new ArrayList<>();
        example = new Example(Topic.class);
        criteria = example.createCriteria();

        if (tagIds != null) {
            String[] id = tagIds.split(",");
            for (String s : id) {
                idList.add(Integer.parseInt(s));
            }
            for (Integer i : idList) {
                criteria.andIn("tagIds", Collections.singleton("%" + i + "%"));
            }
        }

        example.orderBy("topicId").asc();
        List<Topic> topicList = topicMapper.selectByExample(example);
        Set<Topic> uniqueTopics = new LinkedHashSet<>(topicList);
        map.put("tuijian",new ArrayList<>(uniqueTopics));

        example = new Example(Banner.class);
        criteria = example.createCriteria();
        criteria.andIn("bannerFlag", Arrays.asList(1, 3, 5));
        map.put("banner",bannerMapper.selectByExample(example));
        return map;
    }

    @Override
    public PageInfo getRecommend(String tagIds,Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Integer> idList = new ArrayList<>();
        Example example = new Example(Topic.class);
        Example.Criteria criteria = example.createCriteria();

        if (tagIds != null) {
            String[] id = tagIds.split(",");
            for (String s : id) {
                idList.add(Integer.parseInt(s));
            }
            for (Integer i : idList) {
                criteria.andIn("tagIds", Collections.singleton("%" + i + "%"));
            }
        }

        example.orderBy("topicId").asc();
        List<Topic> topicList = topicMapper.selectByExample(example);
        Set<Topic> uniqueTopics = new LinkedHashSet<>(topicList);
        return new PageInfo(new ArrayList<>(uniqueTopics));
    }

    @Override
    public PageInfo getTopic(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = new Example(Topic.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topicFlag", 0);
        return new PageInfo(topicMapper.selectByExample(example));
    }

    @Override
    public PageInfo getVideo(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = new Example(Topic.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topicFlag", 1);
        return new PageInfo(topicMapper.selectByExample(example));
    }

    @Override
    public PageInfo getAppTopic(Integer page, Integer size, String topicTitle) {
        PageHelper.startPage(page,size);
        Example example = new Example(Topic.class);
        Example.Criteria criteria = example.createCriteria();
        if(topicTitle!= null){
            criteria.andLike("topicTitle", "%" + topicTitle + "%");
        }
        return new PageInfo(topicMapper.selectByExample(example));
    }

}
